using System;
using System.Collections;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using VSSDKAssist.Assets.Helpers;
using VSSDKAssist.Assets.ValueProviders.Base;
using Microsoft.Practices.Common;
using VSShellAssist.Recipes.Common.Helpers;

namespace VSShellAssist.Recipes.Common.ValueProviders
{
	/// <summary>
	/// Value provider for a value inside a pkgdef file
	/// </summary>
    public class DefFileKeyValueValueProvider : ValueProviderBase
	{
        #region Constants
        private const string DefFileArgument = "DefFileArgument";
        private const string SectionName = "SectionName";
        private const string KeyName = "KeyName";
        #endregion

        #region Fields
        private string defFileArgument;
        private string sectionName;
        private string keyName;
        #endregion


		#region Public Implementation

		/// <summary>
		/// Contains code that will be called when recipe execution begins. This is the first method in the lifecycle.
		/// </summary>
		/// <param name="currentValue">An <see cref="T:System.Object"/> that contains the current value of the argument.</param>
		/// <param name="newValue">When this method returns, contains an <see cref="T:System.Object"/> that contains
		/// the new value of the argument, if the returned value
		/// of the method is <see langword="true"/>. Otherwise, it is ignored.</param>
		/// <returns>
		/// 	<see langword="true"/> if the argument value should be replaced with
		/// the value in <paramref name="newValue"/>; otherwise, <see langword="false"/>.
		/// </returns>
		/// <remarks>By default, always returns <see langword="false"/>, unless overriden by a derived class.</remarks>
		public override bool OnBeforeActions(object currentValue, out object newValue)
		{
			newValue = null;

            if((currentValue == null) && (this.ExecutionStage != Stage.OnBeginRecipe))
            {
                ProjectItem defFileArgumentValue = DictionaryService.GetValue(defFileArgument) as ProjectItem;

                DefFileReader reader = new DefFileReader(defFileArgumentValue.Properties.Item("FullPath").Value.ToString());

                newValue = reader.GetStringValue(this.sectionName, this.keyName);

                return true;
			}

			return false;
		}
		#endregion

        protected override void OnConfigure(System.Collections.Specialized.StringDictionary attributes)
        {
            if(attributes == null)
                throw new ArgumentNullException("attributes");

            if(attributes.ContainsKey(DefFileArgument))
            {
                defFileArgument = attributes[DefFileArgument];
            }

            if(attributes.ContainsKey(SectionName))
            {
                sectionName = attributes[SectionName];
            }

            if(attributes.ContainsKey(KeyName))
            {
                keyName = attributes[KeyName];
            }
        }
    }
}